<HTML><HEAD><TITLE>merge(+List1, +List2, -List3)</TITLE>
</HEAD><BODY>[ <A HREF="index.html">Comparing and Sorting</A> | <A HREF="../../index.html">Reference Manual</A> | <A HREF="../../fullindex.html">Alphabetic Index</A> ]
<H1>merge(+List1, +List2, -List3)</H1>
Succeeds if List3 is a merged list of List1 and List2.  If both lists are
sorted, List3 will be sorted.


<DL>
<DT><EM>List1</EM></DT>
<DD>List.
</DD>
<DT><EM>List2</EM></DT>
<DD>List.
</DD>
<DT><EM>List3</EM></DT>
<DD>List or variable.
</DD>
</DL>
<H2>Description</H2>
   Used to merge the sorted lists List1 and List2 to give the sorted list
   List3.  merge(L1,L2,L3) is equivalent to merge(0,=&lt;,L1,L2,L3).

<P>
   For two lists [e1,e2,e3] and [f1,f2,f3], e1 is compared to f1.  The
   lower (dictated by the standard ordering below) is put into List3, and
   the process continued with the remaining input lists.  This process
   continues until both lists are exhausted.

<P>
   In particular, this will merge two sorted lists into a sorted list.

<P>
   The sort is done according to the standard ordering of terms.
   Duplicates are not removed.  See compare/3 for this standard ordering.
   Note in particular that numbers are first ordered by their type (integer,
   float, etc) and only then by their magnitude, i.e. sorting numbers of
   different types may not give the expected result.

<P>
   merge(A, B, M) is equivalent to merge(0, =&lt;, A, B, M).
<P>

<H3>Modes and Determinism</H3><UL>
<LI>merge(+, +, -) is det
</UL>
<H2>Examples</H2>
<PRE>
Success:
      merge([2,4,6],[1,3,5],L).
                            (gives L=[1,2,3,4,5,6]).
      merge([f(1),f(7)],[f(8),f(10)],L).
                          (gives L=[f(1),f(7),f(8),f(10)]).
      merge([f(5),f(8)],[f(1),f(2),f(2),f(5),f(8)],L).
            (gives L=[f(1),f(2),f(2),f(5),f(5),f(8),f(8)]).
      merge([a,w],[a,b,b,r,w],L).
            (gives L=[a,a,b,b,r,w,w]).
      merge([f(2),f(1)],[f(3),f(8)],L).
            (gives L=[f(2),f(1),f(3),f(8)]).

Fail:
      merge([2,4,6],[1,3,5],[1,2,3,4,5]).



</PRE>
<H2>See Also</H2>
<A HREF="../../kernel/termcomp/compare-3.html">compare / 3</A>, <A HREF="../../kernel/termcomp/merge-5.html">merge / 5</A>, <A HREF="../../kernel/termcomp/msort-2.html">msort / 2</A>, <A HREF="../../kernel/termcomp/number_merge-3.html">number_merge / 3</A>
</BODY></HTML>
